
package net.pec.geotools_svg.svg_conversion;


import java.io.*;
import java.net.URL;

import org.apache.batik.dom.svg.SVGDOMImplementation;
import org.geotools.data.*;
import org.geotools.feature.*;
import org.opengis.feature.simple.SimpleFeature;

import com.vividsolutions.jts.geom.*;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

public class GeneratePolygonElement {

private String fillColor = "green";
private String strokeColor = "black";
private double middleY;



  
public void transformFeature(SimpleFeature feature, String ID,Document doc, Element gElement, 
		Double midpoint, int fillRed, int fillGreen, int fillBlue, double fillopacity, 
		int strokeRed, int strokeGreen, int strokeBlue,  double strokeopacity, String strokewidth, 
		String[] EventsType, String[] EventNames, String[] additionalAttributeType, String[] additionalAttributeValue  ) throws Exception {
	
	
  middleY = midpoint;
  
  Coordinate[] coord;
  Coordinate oneCrd = new Coordinate();
  
  String svgNS = SVGDOMImplementation.SVG_NAMESPACE_URI;
  Element polygonElement = doc.createElementNS(svgNS, "path");
  polygonElement.setAttributeNS(null, "id", ID);
  polygonElement.setAttributeNS(null, "fill-rule","evenodd");
  polygonElement.setAttributeNS(null, "fill", "rgb("+fillRed+","+fillGreen+","+fillBlue+")");
  polygonElement.setAttributeNS(null, "stroke", "rgb("+strokeRed+","+strokeGreen+","+strokeBlue+")");
  polygonElement.setAttributeNS(null, "stroke-width", strokewidth);
  polygonElement.setAttributeNS(null, "stroke-opacity", Double.toString(strokeopacity));
  polygonElement.setAttributeNS(null, "fill-opacity", Double.toString(fillopacity));
  
  
  if (EventsType !=null){
	  for(int i=0; i<EventsType.length; i++){	
	  polygonElement.setAttributeNS(null, EventsType[i], EventNames[i]);
	  }
	  }
  
  if (additionalAttributeType !=null){
	  for(int i=0; i<additionalAttributeType.length; i++){	
	  polygonElement.setAttributeNS(null, additionalAttributeType[i], additionalAttributeValue[i]);
	  }
	  }
  
  Geometry geom = (Geometry) feature.getDefaultGeometry();
  
  String pointValues = "M";
  
	  if (geom instanceof Polygon ){
	   
	   coord = (((Polygon)geom).getExteriorRing()).getCoordinates();
	   for ( int i = 0 ; i < coord.length ; i++ ){
	    if ( i!= 0 ){
	    	pointValues = pointValues+"L";
	    }
	    double coorddiff = coord[i].y - middleY;
		   double newcoord = middleY-coorddiff;
		   oneCrd = coord[i];
		   pointValues = pointValues+oneCrd.x + "," + newcoord + " ";
	    
	   }
	   pointValues = pointValues+"Z ";
	   
	   for ( int j = 0 ; j < ((Polygon)geom).getNumInteriorRing() ; j++ ){
	    coord = (((Polygon)geom).getInteriorRingN(j)).getCoordinates();
	    for ( int i = 0 ; i < coord.length ; i++ ){
	     if ( i!= 0 ){
	    	 pointValues = pointValues+"L";
	     } else {
	    	 pointValues = pointValues+"M";
	     }
	  	   double coorddiff = coord[i].y - middleY;
	  	   double newcoord = middleY-coorddiff;
	  	   oneCrd = coord[i];
	  	 pointValues = pointValues+oneCrd.x + "," + newcoord + " " ;
	     
	    }
	    pointValues = pointValues+"Z ";
	   }
   
	   polygonElement.setAttributeNS(null, "d", pointValues);
	   gElement.appendChild(polygonElement);
  } 
	  
	  if (geom instanceof MultiPolygon ){		   
		  for ( int j = 0 ; j < ((GeometryCollection)geom).getNumGeometries() ; j++){
			    Geometry childGeom = ((GeometryCollection)geom).getGeometryN(j);
			    transformGeometryCollection(feature, childGeom, doc, ID,gElement,middleY,fillRed, fillGreen,fillBlue,
			    		fillopacity, strokeRed,strokeBlue,strokeGreen,strokeopacity,strokewidth, EventsType, EventNames,additionalAttributeType
			    		,additionalAttributeValue);
			   }		  
	  } 
	  
   
  
   
}

private void transformGeometryCollection(SimpleFeature feature, Geometry geom, Document doc, String ID, Element gElement, Double midpoint, int fillRed, int fillGreen, int fillBlue, double fillopacity, 
		int strokeRed, int strokeGreen, int strokeBlue,  double strokeopacity, String strokewidth,String[] EventsType, String[] EventNames, String[] additionalAttributeType, String[] additionalAttributeValue  ) throws Exception {
	  Coordinate[] coord;
	  Coordinate oneCrd = new Coordinate();
	  String svgNS = SVGDOMImplementation.SVG_NAMESPACE_URI;
	  Element polygonElement = doc.createElementNS(svgNS, "path");
	  polygonElement.setAttributeNS(null, "id", ID);
	  polygonElement.setAttributeNS(null, "fill-rule","evenodd");
	  polygonElement.setAttributeNS(null, "fill", "rgb("+fillRed+","+fillGreen+","+fillBlue+")");
	  polygonElement.setAttributeNS(null, "stroke", "rgb("+strokeRed+","+strokeGreen+","+strokeBlue+")");
	  polygonElement.setAttributeNS(null, "stroke-width", strokewidth);
	  polygonElement.setAttributeNS(null, "stroke-opacity", Double.toString(strokeopacity));
	  polygonElement.setAttributeNS(null, "fill-opacity", Double.toString(fillopacity));
	  
	  if (EventsType !=null){
		  for(int i=0; i<EventsType.length; i++){	
		  polygonElement.setAttributeNS(null, EventsType[i], EventNames[i]);
		  }
		  }
	  
	  if (additionalAttributeType !=null){
		  for(int i=0; i<additionalAttributeType.length; i++){	
		  polygonElement.setAttributeNS(null, additionalAttributeType[i], additionalAttributeValue[i]);
		  }
		  }
	  
	  
	  String pointValues = "M";
	  
		  if (geom instanceof Polygon ){
		   
		   coord = (((Polygon)geom).getExteriorRing()).getCoordinates();
		   for ( int i = 0 ; i < coord.length ; i++ ){
		    if ( i!= 0 ){
		    	pointValues = pointValues+"L";
		    }
		    double coorddiff = coord[i].y - middleY;
			   double newcoord = middleY-coorddiff;
			   oneCrd = coord[i];
			   pointValues = pointValues+oneCrd.x + "," + newcoord + " ";
		    //oneCrd = transform2D(coord[i] );
		    //out.write( oneCrd.x + "," + oneCrd.y + " " );
		   }
		   pointValues = pointValues+"Z ";
		   
		   for ( int j = 0 ; j < ((Polygon)geom).getNumInteriorRing() ; j++ ){
		    coord = (((Polygon)geom).getInteriorRingN(j)).getCoordinates();
		    for ( int i = 0 ; i < coord.length ; i++ ){
		     if ( i!= 0 ){
		    	 pointValues = pointValues+"L";
		     } else {
		    	 pointValues = pointValues+"M";
		     }
		  	   double coorddiff = coord[i].y - middleY;
		  	   double newcoord = middleY-coorddiff;
		  	   oneCrd = coord[i];
		  	 pointValues = pointValues+oneCrd.x + "," + newcoord + " " ;
		     
		    }
		    pointValues = pointValues+"Z ";
		   }
	   
	  } 
		  
			  
		  polygonElement.setAttributeNS(null, "d", pointValues);
		   gElement.appendChild(polygonElement);
	}


}